"""
图片名由国知局id 转换成pub_no
"""
import os
import shutil
import sys
import time


def getcsvsize():
    dicts = {}
    num = 0
    # 数据库导出的状态为1的港澳台中国区内部id与pubid的对应表
    with open(r"./rawid_pubid.csv", "r", encoding="GB2312") as f:
        for line in f:
            num = num + 1
            rawid, pub_id = line.split(",")
            rawid, pub_id = rawid.lower(), pub_id.lower()
            dicts[rawid] = pub_id
            if num % 10000 == 1:
                print(num)

    time.sleep(5)
    print(sys.getsizeof(dicts))
    time.sleep(5)


def getrawid_path():
    dicts = {}
    num = 0
    # 内部id到图片实际位置的对应关系
    for path, dirNames, fileNames in os.walk(r"\\192.168.30.177\样例数据\cnipapatent\cnipapatent_cover_raw_all\00027",
                                             topdown=True):
        # 获取当前目录的所有文件
        for fileName in fileNames:
            filepath = os.path.join(path, fileName)
            # print("\\".join(filepath.split("\\")[-3:]))
            # print(fileName.replace(".jpg", ""))
            dicts[fileName.replace(".jpg", "")] = "\\".join(filepath.split("\\")[-3:])
            num = num + 1
            if num % 10000 == 1:
                print(num)

    with open("./id_filepath.csv", "w", encoding="utf-8") as f:
        [f.write('{0},{1}\n'.format(key, value)) for key, value in dicts.items()]


def getrawid_pubid_filepath():
    # 将两个csv 文件合成一个  以rawid 为键 路径和pubid 为值这样可以得到数据库与实际文件之间的
    # 数据差异
    dicts = {}
    dicts1 = {}
    num = 0
    # 数据库导出的状态为1的港澳台中国区内部id与pubid的对应表
    with open(r"./rawid_pubid.csv", "r", encoding="GB2312") as f:
        for line in f:
            num = num + 1
            rawid, pub_id = line.split(",")
            rawid, pub_id = rawid.lower(), pub_id.lower()
            dicts[rawid] = pub_id
            if num % 10000 == 1:
                print(num)

    with open(r"./id_filepath.csv", "r", encoding="utf-8") as f:
        for line in f:
            num = num + 1
            rawid, filepath = line.split(",")
            rawid, filepath = rawid.lower(), filepath.lower()
            dicts1[rawid] = filepath
            if num % 10000 == 1:
                print(num)
    """
    # 通过比对发现了一些其他国家的图片 转换的时候会抛弃 下面开始清理图片中欧洲等不是中国
    # 的图片
    print(dicts1.keys() - (dicts.keys() & dicts1.keys()))
    """
    with open("./id_pub_filepath.csv", "w", encoding="utf-8") as f:

        for key in dicts.keys() & dicts1.keys():
            # pub_id
            value1 = dicts[key].strip()
            # 文件路径
            value2 = dicts1[key].strip()
            f.write('{0},{1},{2}\n'.format(key, value1, value2))
            if num % 10000 == 1:
                print(num)


def clear():
    """
    清理图片中不是中国区的图片
    :return:
    """
    num = 0
    dicts1 = {}
    with open(r"./id_filepath.csv", "r", encoding="utf-8") as f:
        for line in f:
            num = num + 1
            rawid, filepath = line.split(",")
            rawid, filepath = rawid.lower(), filepath.lower()
            dicts1[rawid] = filepath
            if num % 10000 == 1:
                print(num)
    dicts = {}
    with open(r"./clear.csv", "r", encoding="utf-8") as f:
        for line in f:
            num = num + 1
            rawid, pub_id = line.split(",")
            rawid, pub_id = rawid.lower(), pub_id.lower()
            dicts[rawid] = pub_id
            if num % 10000 == 1:
                print(num)

    for key in dicts:
        filepath = os.path.join(r"\\192.168.30.177\样例数据\cnipapatent\cnipapatent_cover_raw_all\00027",
                                dicts1[key].strip())
        print(filepath)
        os.remove(filepath)


def copy_file():
    """
    将cnipa的文件转换成cnipr的文件 主要是文件id 使用pub_no
    :return:
    """

    with open("./id_pub_filepath.csv", "r", encoding="utf-8") as f:
        for line in f:
            rawid, pub_id, filepath = line.split(",")
            rawid, pub_id, filepath = rawid.strip(), pub_id.strip(), filepath.strip()
            # input_path = os.path.join(r"\\192.168.30.177\样例数据\cnipapatent\cnipapatent_cover_raw_all\00027",
            #                           filepath)
            # output_dir = os.path.join(r"\\192.168.30.177\样例数据\cniprpatent\cniprpatent_cover_raw_20190929",
            #                           pub_id[:3], pub_id[3:7])
            input_path = os.path.join(r"E:\样例数据\cnipapatent\cnipapatent_cover_raw_all\00027",
                                      filepath)
            output_dir = os.path.join(r"E:\样例数据\cniprpatent\cniprpatent_cover_raw_20190929",
                                      pub_id[:3], pub_id[3:7])
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
            output_path = os.path.join(output_dir, pub_id + ".jpg")
            shutil.copyfile(input_path, output_path)
            print(input_path)
            print(output_path)


copy_file()
